﻿using System;
using System.Collections;
using System.Text;
using System.Web;
using System.Web.UI;

namespace Licho.Utils.Web
{
	/// <summary>
	/// Shows javascript popup - works just like normal messagebox
	/// </summary>
	public class MessageBox
	{
		private static readonly Hashtable ExecutingPages = new Hashtable();

		private MessageBox() {}

		public static void Show(string sMessage) {
			// If this is the first time a page has called this method then
			if (!ExecutingPages.Contains(HttpContext.Current.Handler)) {
				// Attempt to cast HttpHandler as a Page.
				var executingPage = HttpContext.Current.Handler as Page;

				if (executingPage != null) {
					// Create a Queue to hold one or more messages.
					var messageQueue = new Queue();

					// Add our message to the Queue
					messageQueue.Enqueue(sMessage);

					// Add our message queue to the hash table. Use our page reference
					// (IHttpHandler) as the key.
					ExecutingPages.Add(HttpContext.Current.Handler, messageQueue);

					// Wire up Unload event so that we can inject 
					// some JavaScript for the alerts.
					executingPage.Unload += ExecutingPageUnload;
				}
			}
			else {
				// If were here then the method has allready been 
				// called from the executing Page.
				// We have allready created a message queue and stored a
				// reference to it in our hastable. 
				var queue = (Queue)ExecutingPages[HttpContext.Current.Handler];

				// Add our message to the Queue
				queue.Enqueue(sMessage);
			}
		}

		// Our page has finished rendering so lets output the
		// JavaScript to produce the alert's
		private static void ExecutingPageUnload(object sender, EventArgs e) {
			// Get our message queue from the hashtable
			var queue = (Queue)ExecutingPages[HttpContext.Current.Handler];

			if (queue != null) {
				var sb = new StringBuilder();

				// How many messages have been registered?
				int iMsgCount = queue.Count;

				// Use StringBuilder to build up our client slide JavaScript.
				sb.Append("<script language='javascript'>");

				// Loop round registered messages
				string sMsg;
				while (iMsgCount-- > 0) {
					sMsg = (string)queue.Dequeue();
					sMsg = sMsg.Replace("\\", "\\\\");
					sMsg = sMsg.Replace("\n", "\\n");
					sMsg = sMsg.Replace("\r", "\\r");
					sMsg = sMsg.Replace("\"", "'");
					sb.Append(@"alert( """ + sMsg + @""" );");
				}

				// Close our JS
				sb.Append(@"</script>");

				// Were done, so remove our page reference from the hashtable
				ExecutingPages.Remove(HttpContext.Current.Handler);

				// Write the JavaScript to the end of the response stream.
				HttpContext.Current.Response.Write(sb.ToString());
			}
		}
	}
}